package com.itheima.sh.b_response_02;


import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/responseDemo04Servlet")
public class ResponseDemo04Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       /*
            需求：给浏览器响应字节数据。
                将D:\abc\柳岩.jpg输出到浏览器
        */

        FileInputStream fis = new FileInputStream("/Users/laoyao/Pictures/截图/截屏2022-06-28 21.43.45.png");

        ServletOutputStream os = response.getOutputStream();

        byte[] bytes = new byte[1024];
        int len;
        while ((len=fis.read(bytes))!=-1){
            os.write(bytes,0,len);
        }


        fis.close();

    }
}
